package edu.udo.sopra10.chaturaji.entities.transfer;

import static edu.udo.sopra10.chaturaji.util.ExceptionUtil.assertNotNull;
import edu.udo.sopra10.chaturaji.entities.AbstractGame;
import edu.udo.sopra10.chaturaji.entities.Game;
import edu.udo.sopra10.chaturaji.entities.Square;
import edu.udo.sopra10.chaturaji.entities.coordinates.Position;
import edu.udo.sopra10.chaturaji.entities.pieces.Piece;
import edu.udo.sopra10.chaturaji.entities.pieces.Piece.Actions;

/**
 * Transfer object of {@link Game}, with copies of all squares and pieces.
 * 
 * @author Patrick Westerhoff
 */
public class TransferGame extends AbstractGame
{
	/**
	 * Constructs a new TransferGame object based on the original game.
	 * 
	 * @param game the original game.
	 */
	public TransferGame ( Game game )
	{
		this.players.addAll( game.getPlayers() );
		
		Square[][] board = game.getBoard();
		
		for ( byte i = 0; i < 8; i++ )
		{
			for ( byte j = 0; j < 8; j++ )
			{
				Square square = new Square( this, new Position( i, j ) );
				
				if ( board[i][j].getPiece() != null )
				{
					Piece piece = (Piece) board[i][j].getPiece().clone();
					piece.setSquare( square );
					square.setPiece( piece );
				}
				
				this.board[i][j] = square;
			}
		}
	}
	
	/**
	 * Get all possible actions of a piece at the given position. Returns
	 * <code>null</code> if there is no piece at that position.
	 * 
	 * @param position position of the piece.
	 * @return all possible actions of the piece or null if not a piece.
	 */
	public Actions getPossibleActions ( final Position position )
	{
		assertNotNull( position, "position" );
		final Piece piece = board[position.getX()][position.getY()].getPiece();
		
		if ( piece == null )
			return null;
		
		return piece.getPossibleActions();
	}
	
	/**
	 * Returns the piece at the given position.
	 * 
	 * @param position position of the square.
	 * @return piece or <code>null</code>.
	 */
	public Piece getPieceAt ( final Position position )
	{
		return board[position.getX()][position.getY()].getPiece();
	}
	
	/**
	 * Sets the piece at the given position.
	 * 
	 * @param position position of the square.
	 * @param piece the piece to set.
	 */
	public void setPieceAt ( final Position position, Piece piece )
	{
		board[position.getX()][position.getY()].setPiece( piece );
	}
}